/**
 * Created by Administrator on 2017/5/1.
 */
var submitButton=document.getElementById('submit');
var messageElement=document.getElementById('message');
var myFormElement=document.getElementById('myForm');
submitButton.onclick=function(e) {
    e.preventDefault();

    var passwordElement = document.querySelector('#myForm input[name=password]');
    var password = passwordElement.value;
    if (password == "") {
        messageElement.innerHTML = '密码不能为空';
        return;
    };

    var confirmElement = document.querySelector('#myForm input[name=confirm]');
    var confirm= confirmElement.value;
    if (confirm == "") {
        messageElement.innerHTML = '确认密码不能为空';
        return;
    }

    var nicknameElement = document.querySelector('#myForm input[name=nickname]');
    var nickname= nicknameElement.value;
    if (nickname == "") {
        messageElement.innerHTML = '昵称不能为空';
        return;
    }

    var userImgElement = document.querySelector('#myForm input[name=image]');
    var userImg = userImgElement.value;
    if (userImg == "") {
        messageElement.innerHTML = '头像不能为空';
        return;
    }
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (this.readyState == 4) {
            if (this.status == 200) {
                var result = JSON.parse(this.responseText);
                if (result.error == 1) {
                    messageElement.innerHTML = result.message;
                } else {
                    messageElement.innerHTML = "修改成功";
                    location.href = 'Index.php';
                }
            } else {
                alert("程序出现异常");
            }
        }
    };
    xhr.open(myFormElement.method, myFormElement.action);
    var data = new FormData();
    data.append('password', password);
    data.append('confirm', confirm);
    data.append('nickname', nickname);
    data.append('image', userImgElement.files[0]);
    xhr.send(data);
};
